using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using MacomberMap.Common.Components;
using MacomberMap.Common.Database_Interface.CIM.Components;

namespace MacomberMap.Common.Database_Interface.CIM.User_Interfaces
{
    /// <summary>
    /// This class holds the front end of a CIM element (to be shown in the CIM browser)
    /// </summary>
    public partial class CIM_Element_FrontEnd : Control
    {
        #region Variable Declarations
        /// <summary>The element upon which this control is based</summary>
        public CIM_Element BaseElement;

        /// <summary>Our forward-facing list of elements</summary>
        public List<CIM_Element_FrontEnd> OutgoingElements = new List<CIM_Element_FrontEnd>(10);
        #endregion

        #region Initialization
        /// <summary>
        /// Initialize a new CIM element
        /// </summary>
        /// <param name="BaseElement">The CIM element upon which the element is based</param>
        public CIM_Element_FrontEnd(CIM_Element BaseElement)
        {
            this.BaseElement = BaseElement;
            this.BackColor = Color.DarkSlateBlue;
            this.ForeColor = Color.White;
          
            InitializeComponent();            
        }

        /// <summary>
        /// Recompute the text for an element, and auto-size it (when user preferences change)
        /// </summary>        
        /// <param name="ShowRdfID">Whether rdf:ID should be displayed</param>
        /// <param name="ShowType">Whether type should be displayed</param>
        /// <param name="ShowName">Whether name should be displayed</param>
        /// <param name="ShowTEID">Whether TEID should be displayed</param>
        public void RecomputeText(bool ShowRdfID, bool ShowType, bool ShowName, bool ShowTEID)
        {
            StringBuilder OutText = new StringBuilder();
            if (ShowRdfID)
                OutText.AppendLine(BaseElement.rdfID.ToString());
            if (ShowType)
                OutText.AppendLine(BaseElement.Type.Substring(BaseElement.Type.IndexOf(':')+1));
            if (ShowName)
                OutText.AppendLine(BaseElement.Name);
            if (ShowTEID)
            {
                UInt64 TEID;
                if (MM_Database_CIM.ReverseTEIDs.TryGetValue(BaseElement, out TEID))
                    OutText.AppendLine(TEID.ToString("#,##0"));
            }
            this.Text = OutText.ToString().Trim(' ', '\n');

            using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
                this.Size = Size.Ceiling(g.MeasureString(this.Text, this.Font));
            this.Invalidate();
        }

        #endregion   
     
        #region Rendering
        /// <summary>
        /// Handle the painting of our element
        /// </summary>
        /// <param name="e"></param>
        protected override void OnPaint(PaintEventArgs e)
        {
            StringFormat sF = new StringFormat();
            sF.Alignment = StringAlignment.Center;
            sF.LineAlignment = StringAlignment.Center;

            
            if ((BaseElement.Type == "cim:Breaker" || BaseElement.Type == "cim:Disconnector") && bool.Parse(BaseElement["cim:Switch.normalOpen"]))
            {
                e.Graphics.Clear(Color.DarkGreen);
                e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), this.Width / 2f, this.Height / 2f, sF);
            }
            else if (BaseElement.Type == "cim:Breaker" || BaseElement.Type == "cim:ACLineSegment" || BaseElement.Type == "cim:Line")
            {
                e.Graphics.Clear(this.ForeColor);
                e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.BackColor), this.Width / 2f, this.Height / 2f, sF);
            }
            else
            {
                e.Graphics.Clear(Color.DarkGray);
                e.Graphics.DrawString(this.Text, this.Font, new SolidBrush(this.ForeColor), this.Width / 2f, this.Height / 2f, sF);
            }
            
        }
        #endregion
    }
}
